package com.smart.translator.ui.settings;

import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.smart.translator.R;
import com.smart.translator.ui.LoginActivity;
import com.smart.translator.util.UserManager;

public class SettingsViewModel extends AndroidViewModel {
    private static final String TAG = "SettingsViewModel";
    private static final String KEY_FLOAT_WINDOW = "float_window";
    private static final String KEY_USER_NAME = "user_name";

    private final MutableLiveData<Boolean> floatWindow = new MutableLiveData<>(false);
    private final MutableLiveData<String> userName = new MutableLiveData<>("加载中...");
    private final MutableLiveData<String> version = new MutableLiveData<>("当前版本 1.0.0");

    private final SharedPreferences preferences;
    private final UserManager userManager;

    public SettingsViewModel(@NonNull Application application) {
        super(application);
        preferences = PreferenceManager.getDefaultSharedPreferences(application);
        userManager = UserManager.getInstance(application);
        loadSettings();
    }

    private void loadSettings() {
        floatWindow.setValue(preferences.getBoolean(KEY_FLOAT_WINDOW, false));
        
        // 从UserManager获取用户名
        String displayName = userManager.getDisplayName();
        userName.setValue(displayName);
    }

    public LiveData<Boolean> getFloatWindow() {
        return floatWindow;
    }

    public LiveData<String> getUserName() {
        return userName;
    }

    public LiveData<String> getVersion() {
        return version;
    }

    public void setFloatWindow(boolean enabled) {
        floatWindow.setValue(enabled);
        preferences.edit().putBoolean(KEY_FLOAT_WINDOW, enabled).apply();
    }

    public void setUserName(String name) {
        userName.setValue(name);
        preferences.edit().putString(KEY_USER_NAME, name).apply();
    }

    public void checkUpdate() {
        // 这里应该实现检查更新的逻辑
        // 例如：调用服务器API检查是否有新版本
        Toast.makeText(getApplication(), "当前已是最新版本", Toast.LENGTH_SHORT).show();
    }

    public void showAbout() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getApplication().getApplicationContext());
        builder.setView(R.layout.dialog_about);
        builder.setPositiveButton("确定", (dialog, which) -> dialog.dismiss());
        builder.create().show();
    }

    public void handleLogout() {
        // 直接执行退出登录，不显示确认对话框
        // 清除用户信息
        userManager.logout();
        
        // 清除本地设置中的用户名
        preferences.edit()
            .remove(KEY_USER_NAME)
            .apply();
        
        // 重置用户名显示
        userName.setValue("未登录");
        
        // 显示提示
        Toast.makeText(getApplication(), "已退出登录", Toast.LENGTH_SHORT).show();
        
        // 跳转到登录页面
        Intent intent = new Intent(getApplication(), LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        getApplication().startActivity(intent);
    }
} 